package red.social;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class ActualizarMiPosicion extends MapActivity {
	MapView mapView;
	MapController mc;
	Bundle parametros;
	int idusuario;
	private LocationManager locationManager;
	List<Overlay> mapOverlays;
	ItemizedOverlay itemizedOverlay;
	private Drawable drawable;
	String ip;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapa);
		ip  = getResources().getString(R.string.ip);
		parametros = getIntent().getExtras();
		idusuario = parametros.getInt("idUsuario");

		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(true);

		mc = mapView.getController();
		mc.setZoom(4);
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, new GeoUpdateHandler());

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public class GeoUpdateHandler implements LocationListener {

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		@Override
		public void onLocationChanged(Location location) {
			mapOverlays = mapView.getOverlays();
			if (!mapOverlays.isEmpty()) {
				mapOverlays.clear();
			}

			drawable = getResources().getDrawable(R.drawable.androidmarker);
			itemizedOverlay = new ItemizedOverlay(drawable);
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);

			GeoPoint point = new GeoPoint(lat, lng);
			OverlayItem overlayitemUsuario = new OverlayItem(point, "", "");

			itemizedOverlay.addOverlay(overlayitemUsuario);
			mapOverlays.add(itemizedOverlay);
			mc.setCenter(point);
			try {
				
				String servletURL = "http://" + ip
						+ ":8080/Modulo10-war/ActulizarPosicion";
				URL ob = new URL(servletURL + "?lat=" + location.getLatitude()
						* 1E6 + "&lon=" + location.getLongitude() * 1E6
						+ "&idusuario=" + idusuario );
				URLConnection conn = ob.openConnection();
				conn.setDoInput(true);
				conn.setDoOutput(true);
				InputStream in = conn.getInputStream();
				DataInputStream dos = new DataInputStream(in);
				String ok = dos.readLine();
				if (ok.equals("0")) {
					CharSequence text = "Ocurrió un error al actulizar su posición. Reinicie su aplicación, por favor";
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(ActualizarMiPosicion.this, text,
							duration);
					toast.show();
				}
				dos.close();
				conn = null;
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
	}
}

